local joy__kangge = fk.CreateSkill {

  name = "joy__kangge",

  tags = {  },

}



joy__kangge:addEffect(fk.TurnStart, {
  name = "joy__kangge",
  events = {fk.TurnStart, fk.AfterCardsMove, fk.EnterDying},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joy__kangge.name) then
      if event == fk.TurnStart then
        return target == player
      elseif event == fk.AfterCardsMove then
        if player:getMark("joy__kangge_draw-turn") < 3 then
          local n = 0
          for _, move in ipairs(data) do
            if move.to and move.toArea == Card.PlayerHand and player.room:getPlayerById(move.to):getMark("@@joy__kangge") > 0
            and player.room:getPlayerById(move.to).phase == Player.NotActive then
              n = n + #move.moveInfo
            end
          end
          if n > 0 then
            event:setCostData(self, n)
            return true
          end
        end
      else
        return target.dying and target:getMark("@@joy__kangge") > 0 and player:getMark("joy__kangge_help-round") == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EnterDying then
      return player.room:askForSkillInvoke(player, joy__kangge.name, nil, "#joy__kangge-invoke::"..target.id)
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(joy__kangge.name)
    if event == fk.TurnStart then
      room:notifySkillInvoked(player, joy__kangge.name, "special")
      local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
      if #targets == 0 then return end
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joy__kangge") > 0 then
          room:setPlayerMark(p, "@@joy__kangge", 0)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#joy__kangge-choose", joy__kangge.name, false)
      room:setPlayerMark(room:getPlayerById(tos[1]), "@@joy__kangge", 1)
    elseif event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, joy__kangge.name, "drawcard")
      local n = math.min(event:getCostData(self), 3 - player:getMark("joy__kangge_draw-turn"))
      room:addPlayerMark(player, "joy__kangge_draw-turn", n)
      player:drawCards(n, joy__kangge.name)
    else
      room:notifySkillInvoked(player, "joy__kangge", "support")
      room:doIndicate(player.id, {target.id})
      room:setPlayerMark(player, "joy__kangge_help-round", 1)
      room:recover({
        who = target,
        num = 1 - target.hp,
        recoverBy = player,
        skillName = joy__kangge.name,
      })
    end
  end,
})
joy__kangge:addEffect(fk.AfterCardsMove, {
  name = "joy__kangge",
  events = {fk.TurnStart, fk.AfterCardsMove, fk.EnterDying},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joy__kangge.name) then
      if event == fk.TurnStart then
        return target == player
      elseif event == fk.AfterCardsMove then
        if player:getMark("joy__kangge_draw-turn") < 3 then
          local n = 0
          for _, move in ipairs(data) do
            if move.to and move.toArea == Card.PlayerHand and player.room:getPlayerById(move.to):getMark("@@joy__kangge") > 0
            and player.room:getPlayerById(move.to).phase == Player.NotActive then
              n = n + #move.moveInfo
            end
          end
          if n > 0 then
            event:setCostData(self, n)
            return true
          end
        end
      else
        return target.dying and target:getMark("@@joy__kangge") > 0 and player:getMark("joy__kangge_help-round") == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EnterDying then
      return player.room:askForSkillInvoke(player, joy__kangge.name, nil, "#joy__kangge-invoke::"..target.id)
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(joy__kangge.name)
    if event == fk.TurnStart then
      room:notifySkillInvoked(player, joy__kangge.name, "special")
      local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
      if #targets == 0 then return end
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joy__kangge") > 0 then
          room:setPlayerMark(p, "@@joy__kangge", 0)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#joy__kangge-choose", joy__kangge.name, false)
      room:setPlayerMark(room:getPlayerById(tos[1]), "@@joy__kangge", 1)
    elseif event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, joy__kangge.name, "drawcard")
      local n = math.min(event:getCostData(self), 3 - player:getMark("joy__kangge_draw-turn"))
      room:addPlayerMark(player, "joy__kangge_draw-turn", n)
      player:drawCards(n, joy__kangge.name)
    else
      room:notifySkillInvoked(player, "joy__kangge", "support")
      room:doIndicate(player.id, {target.id})
      room:setPlayerMark(player, "joy__kangge_help-round", 1)
      room:recover({
        who = target,
        num = 1 - target.hp,
        recoverBy = player,
        skillName = joy__kangge.name,
      })
    end
  end,
})
joy__kangge:addEffect(fk.EnterDying, {
  name = "joy__kangge",
  events = {fk.TurnStart, fk.AfterCardsMove, fk.EnterDying},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joy__kangge.name) then
      if event == fk.TurnStart then
        return target == player
      elseif event == fk.AfterCardsMove then
        if player:getMark("joy__kangge_draw-turn") < 3 then
          local n = 0
          for _, move in ipairs(data) do
            if move.to and move.toArea == Card.PlayerHand and player.room:getPlayerById(move.to):getMark("@@joy__kangge") > 0
            and player.room:getPlayerById(move.to).phase == Player.NotActive then
              n = n + #move.moveInfo
            end
          end
          if n > 0 then
            event:setCostData(self, n)
            return true
          end
        end
      else
        return target.dying and target:getMark("@@joy__kangge") > 0 and player:getMark("joy__kangge_help-round") == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EnterDying then
      return player.room:askForSkillInvoke(player, joy__kangge.name, nil, "#joy__kangge-invoke::"..target.id)
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(joy__kangge.name)
    if event == fk.TurnStart then
      room:notifySkillInvoked(player, joy__kangge.name, "special")
      local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
      if #targets == 0 then return end
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joy__kangge") > 0 then
          room:setPlayerMark(p, "@@joy__kangge", 0)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#joy__kangge-choose", joy__kangge.name, false)
      room:setPlayerMark(room:getPlayerById(tos[1]), "@@joy__kangge", 1)
    elseif event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, joy__kangge.name, "drawcard")
      local n = math.min(event:getCostData(self), 3 - player:getMark("joy__kangge_draw-turn"))
      room:addPlayerMark(player, "joy__kangge_draw-turn", n)
      player:drawCards(n, joy__kangge.name)
    else
      room:notifySkillInvoked(player, "joy__kangge", "support")
      room:doIndicate(player.id, {target.id})
      room:setPlayerMark(player, "joy__kangge_help-round", 1)
      room:recover({
        who = target,
        num = 1 - target.hp,
        recoverBy = player,
        skillName = joy__kangge.name,
      })
    end
  end,
})

return joy__kangge